package com.service;

import java.util.ArrayList;
import java.util.Collection;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.authority.GrantedAuthorityImpl;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.dao.UserDao;
import com.model.CFUser;

@Service("userDetailsService")
@Transactional(readOnly=true)
public class UserDetailsServiceImpl implements UserDetailsService {

	@Autowired
	private UserService userDao;
	
	@Autowired 
	private Assembler assembler;
	
	
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException,NullPointerException {
      
        // loading users object and authorities object belonging to username
        CFUser u = userDao.findUserByUserName(username);
        System.out.print(u);
        if (u == null) {
            throw new UsernameNotFoundException("Username does not exist");
        } 
        return assembler.buildUserFromUserEntity(u);
       
    }

}
